package leetcode

import kotlinetc.createNodeList
import kotlinetc.println
import kotlinetc.toList

//https://leetcode.com/problems/remove-duplicates-from-sorted-list/
/**
Share
Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2
Example 2:

Input: 1->1->2->3->3
Output: 1->2->3
Accepted 348,604   Submissions  809,331
 */
fun main(args: Array<String>) {

    val head = createNodeList("3->3")

    deleteDuplicates1(head).toList().println()
}

fun deleteDuplicates1(head: ListNode?): ListNode? {


/*    val root = ListNode(0)

    root.next = head
    var p = head
    var prev = root
    var dup = false

    while (p != null) {

        if (p.next?.`val` == p.`val`) {
            dup = true
        } else {
            if (dup) {

                dup = false
            }

            prev.next = p
            prev = p


        }

        p = p.next

    }
    return root.next*/


    var p = head

    loop@ while (p != null) {

        when {
            p.next == null -> break@loop
            p.next!!.`val` == p.`val` -> p.next = p.next?.next
            else -> p = p.next
        }

    }
    return head
}